package com.yunwx.wxencapsulation.controller;

import com.alibaba.fastjson.JSONObject;
import com.yunwx.wechat4j.Wechat;
import com.yunwx.wechat4j.api.WebWeixinApi;
import com.yunwx.wechat4j.util.QRCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Map;

@Controller
public class LoginWXController {
    public String uuid="";

    /**
     * @api {get} / 系统默认跳转地址
     * @apiName welcome
     * @apiGroup System-logincontroller
     */
    @GetMapping({"/", ""})
    String welcome(Model model) {
        return "redirect:/login";
    }
    /**
     * @api {get} /login  跳转系统登陆
     * @apiName login
     * @apiGroup System-logincontroller
     */
    @GetMapping("/login")
    String login() {
        return "login";
    }
    /**
     * @api {get} / 获取登录二维码
     * @apiName welcome
     * @apiGroup System-logincontroller
     */
    @GetMapping({"/login/info"})
    void login2(Model model,   HttpServletRequest request,
                  HttpServletResponse response) {
        WebWeixinApi api = new WebWeixinApi();
        Wechat wechat = new Wechat();
        wechat.setWebWeixinApi(api);
       Map map  =   wechat.logtu2(null,false);
       this.uuid= (String) map.get("uuid");
       File file= QRCodeUtil.writeToTempFile((byte[]) map.get("bt"));
        FileInputStream inputStream =null;
        try {
            response.setContentType("image/png");
            inputStream = new FileInputStream(file);
            byte[] data = new byte[(int)file.length()];
            inputStream.read(data);
            inputStream.close();
            OutputStream stream = response.getOutputStream();
            stream.write(data);
            stream.flush();
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @GetMapping({"/login/jiankong"})
    @ResponseBody
    String  jiankong(Model model,   HttpServletRequest request,
                HttpServletResponse response) {

        WebWeixinApi api = new WebWeixinApi();
        Wechat wechat = new Wechat();
        wechat.setWebWeixinApi(api);
        JSONObject result = null;
        result= wechat.waitForConfirm(this.uuid);
        if(result==null){
            return "1";
        }else{
            String   urlVersion = result.getString("urlVersion");
            boolean istrue=false;
            boolean istrue2 =false;
            if(urlVersion!=null){

                istrue  = wechat.getLoginCode(urlVersion);
                if(istrue){
                    istrue2 = wechat.wxInitWithRetry();
                }
                if(istrue2){
                    return "0";
                }else{
                    return "1";
                }
            }else{
                return "1";
            }


        }


    }
}
